package com.hustzp.com.xichuangzhu.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：闫伟强。
 * 日期：2016/6/2。
 */
public class ListViewIndex extends View {

    private List<Index> indexMap;
    private Map<Rect, String> rectMap;
    private Paint paint;
    private float h;
    private float uh;
    private float textHeight;
    private float verticalSpace;
    private int margin = 400;
    private OnIndexTouchListener onIndexTouchListener;

    public void setOnIndexTouchListener(OnIndexTouchListener onIndexTouchListener) {
        this.onIndexTouchListener = onIndexTouchListener;
    }

    public ListViewIndex(Context context) {
        super(context);
        init();
    }

    public ListViewIndex(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ListViewIndex(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ListViewIndex(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setBackgroundColor(0xddffffff);
        indexMap = new ArrayList<>();
        rectMap = new HashMap<>();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setColor(Color.parseColor("#0c80ff"));
        paint.setTextSize(14 * getResources().getDisplayMetrics().density);
        h = getWindowHeight();
        textHeight = getFontHeight(paint.getTextSize());
        verticalSpace = 20 * getResources().getDisplayMetrics().density;
    }

    public void initIndexMap(List<Index> indexMap) {
        this.indexMap = indexMap;
        uh = (h - margin * 2) / indexMap.size();
        rectMap.clear();
        postInvalidate();
    }

    public int getWindowHeight() {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), (int) h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int i = 0;
        //屏幕一半
        int wCenter = getWindowHeight() / 2;
        int fTop = wCenter - (int) uh * indexMap.size() / 2 - margin / 2;
        for (Index index : indexMap) {
            String s = index.getWord();
            float textWidth = paint.measureText(s, 0, s.length());
            float x = (getMeasuredWidth() - textWidth) / 2;
            //居中绘制
            float y = fTop + i * uh;
            canvas.drawText(s, x, y, paint);
            @SuppressLint("DrawAllocation") Rect rect = new Rect(0, (int) (fTop + i * uh - uh), getMeasuredWidth(), (int) (fTop + i * uh));
            rectMap.put(rect, s);
            i++;
        }

        /*paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(0, 0, getMeasuredWidth(), h, paint);*/
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                setBackgroundColor(0xddffffff);
                float x = event.getX();
                float y = event.getY();
                for (Rect rect : rectMap.keySet()) {
                    if (rect.contains((int) x, (int) y)) {
                        String s = rectMap.get(rect);
                        int position = 0;
                        for (Index index : indexMap) {
                            if (index.word.equals(s)) {
                                position = index.position;
                            }
                        }
                        if (onIndexTouchListener != null) {
                            onIndexTouchListener.onIndexTouched(s);
                            onIndexTouchListener.onMappedPosition(position);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                setBackgroundColor(0xddffffff);
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        return super.onTouchEvent(event);
    }

    public int getFontHeight(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    public interface OnIndexTouchListener {
        void onIndexTouched(String s);

        void onMappedPosition(int position);
    }

    public static class Index {
        private String word;
        private int position;

        public Index(String word, int position) {
            this.word = word;
            this.position = position;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
}
